<?php
class Member extends BaseModel implements Retrievable {

	var $first_name;
	var $last_name;
	var $login;
	var $password;
	var $email;
	
    public function Member($values) {
        parent::__construct($values);
    }

    public function validate() {
        $errors = Array();
        if($this->first_name == '') {
        	$errors[] = "First name";
        }
        if($this->last_name == '') {
        	$errors[] = "Last name";
        }
        if($this->login == '') {
        	$errors[] = "Desired Member ID";
        } else {
        	$existing = Member::get($this->login, "login");
        	if($existing->id > 0) {
        		$errors[] = "Member ID is already taken choose another one";
        	}
        }
        if($this->password == '') {
        	$errors[] = "Password";
        	if($this->password2 == '') {
        		$errors[] = "Confirmation Password";
        	}
        } else if($this->password != $this->password2) {
        	$errors[] = "Confirmation Password must match with Password";
        }
        if($this->email == '') {
        	$errors[] = "Email Address";
        }
		return (count($errors) === 0 ? true : $errors);
    }

    public static function get($id, $primaryKey='id') {
        return parent::get($id, __CLASS__, $primaryKey);
    }
    
    public static function getAll($searchArray=Array(), $sortArray=Array(), $limit=0) {
        return parent::getAll($searchArray, $sortArray, __CLASS__, $limit);
    }
    
    public static function isMemberLoggedIn() {
    	return isset($_SESSION['member']);
    }
    
    public static function checkCookie() {    
    	if(isset($_COOKIE['member'])) {
    		$member = Member::get($_COOKIE['member'], 'crc32(id)');
    		if($member->id > 0) {
    			$_SESSION['member'] = $member->id;
    		}
    	}
    }
    
    public static function saveMemberCookie($value) {
    	setcookie("member", sprintf('%u', crc32($value)), time()+(30*24*60*60), "/", str_replace('www', '', $_SERVER['HTTP_HOST']));
    }
    
}
?>